﻿using System;
using System.Collections.Generic;
using System.Linq;
using RESTController;
using RestController.SampleWeb.Models;

namespace RestController.SampleWeb.Controllers
{
    public class CategoriesController : ResourceController<Category>
    {
        private readonly SampleContainer sampleContainer;        
 
        public CategoriesController()
        {
            sampleContainer = new SampleContainer();
        }

        protected override object AddModel(Category model)
        {            
            sampleContainer.Categories.AddObject(model);
            sampleContainer.SaveChanges();
            return model.Id;
        }

        protected override void DeleteModel(object id)
        {     
            throw new InvalidOperationException("Deletes are not supported!");
            //var category = GetModel(id);
            //sampleContainer.Categories.DeleteObject(category);
            //sampleContainer.SaveChanges();
        }

        protected override int GetCount()
        {
            return sampleContainer.Categories.Count();
        }

        protected override Category GetModel(object id)
        {
            var intId = Convert.ToInt32(id);
            return sampleContainer.Categories.First(x => x.Id == intId);
        }

        protected override IEnumerable<Category> GetModels()
        {
            return sampleContainer.Categories.OrderBy(x => x.Name);
        }

        protected override void SaveModel(Category model)
        {
            sampleContainer.SaveChanges();
        }
    }
}
